package com.caigou.controller.front;

import com.caigou.bean.exam.entity.Users;
import com.caigou.bean.exam.param.UserDetailParam;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.IUsersService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/front/user/")
public class FrontUserController {

    @Resource
    private IUsersService usersService;

    @RequestMapping("/getUserDetail")
    public Result getUserDetail(@CurrentUser Users user) {
        String userId = user.getUserid();
        return usersService.getUserDetailByUserId(userId);
    }

    @RequestMapping("/updateUserDetail")
    public Result updateUserDetail(@RequestBody UserDetailParam param, @CurrentUser Users user) {
        if (param.getCompany() != null && param.getCompany().length() > 255) {
            return Result.error("公司名称不能超过255个字符", "公司名称不能超过255个字符");
        }
        if (param.getDepart() != null && param.getDepart().length() > 20) {
            return Result.error("部门不能超过20个字符", "部门不能超过20个字符");
        }
        if (param.getWorker() != null && param.getWorker().length() > 20) {
            return Result.error("职务不能超过20个字符", "职务不能超过20个字符");
        }
        if (param.getUserSignature() != null && param.getUserSignature().length() > 255) {
            return Result.error("个人简介不能超过255个字符", "个人简介不能超过255个字符");
        }
        String userId = user.getUserid();
        param.setUserId(userId);
        return usersService.updateUserDetail(param);
    }


    @RequestMapping("/updateNickname")
    public Result updateNickname(@RequestBody UserDetailParam param, @CurrentUser Users user) {
        if (param.getNickname() != null && param.getNickname().length() > 60) {
            return Result.error("昵称不能超过60个字符", "昵称不能超过60个字符");
        }
        String userId = user.getUserid();
        param.setUserId(userId);
        return usersService.updateUserDetail(param);
    }

    @RequestMapping("uploadImage")
    public Result uploadImage(@RequestParam(value = "file") MultipartFile file, @CurrentUser Users user) {
        if (file == null) {
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return usersService.uploadImage(file, user.getUserid());
    }
}
